package com.vision.controller;

import com.vision.pojo.AjaxResult;
import com.vision.pojo.vo.CommentVO;
import com.vision.pojo.vo.PageVO;
import com.vision.service.CommentService;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotEmpty;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @projectName: vision
 * @package: com.vision.controller
 * @className: CommentController
 * @author: escapeicon@gmail.com
 * @description: 评论控制器
 * @date: 2024/1/25 20:26
 * @version: 1.0
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * @param videoId 视频id
     * @param content 评论内容
     * @param parentCommentId 父评论id
     * @param replyUserId 回复用户id
     * @return com.vision.pojo.AjaxResult
     * @description: 添加评论
     * @date: 2024/1/25 20:27
     */
    @PostMapping("/addComment")
    public AjaxResult<CommentVO> addComment (
            @NotNull Integer videoId,
            @NotEmpty String content,
            Integer parentCommentId,
            Integer replyUserId
    ) {
        return AjaxResult.success(commentService.addComment(videoId,content,parentCommentId,replyUserId), "评论成功");
    }

    /**
     * @param pageNum 页码
     * @param pageSize 页大小
     * @param videoId 视频id
     * @return com.vision.pojo.AjaxResult<com.vision.pojo.vo.PageVO<com.vision.pojo.vo.CommentVO>>
     * @description: 获取父评论
     * @date: 2024/1/25 20:28
     */
    @GetMapping("/getParentComment")
    public AjaxResult<PageVO<CommentVO>> getParentComment(
            @NotNull Integer pageNum,
            @NotNull Integer pageSize,
            @NotNull Integer videoId,
            HttpSession session
    ){
        // 1. 获取用户id
        Map<String, Integer> claims = (Map<String, Integer>) session.getAttribute("claims");
        Integer userId = null;
        if (claims != null) {
            userId = claims.get("id");
        }
        return AjaxResult.success(commentService.getParentComment(pageNum,pageSize,videoId,userId));
    }

    /**
     * @description: 获取子评论
     * @param pageNum 页码
     * @param pageSize 页大小
     * @param videoId 视频id
     * @param parentId 父评论id
     * @param session 会话
     * @return com.vision.pojo.AjaxResult<com.vision.pojo.vo.PageVO<com.vision.pojo.vo.CommentVO>>
     */
    @GetMapping("/getSonComment")
    public AjaxResult<PageVO<CommentVO>> getSonComment(
            @NotNull Integer pageNum,
            @NotNull Integer pageSize,
            @NotNull Integer videoId,
            @NotNull Integer parentId,
            HttpSession session
    ){
        // 1. 获取用户id
        Map<String, Integer> claims = (Map<String, Integer>) session.getAttribute("claims");
        Integer userId = null;
        if (claims != null) {
            userId = claims.get("id");
        }
        return AjaxResult.success(commentService.getSonComment(pageNum,pageSize,videoId,parentId,userId));
    }
}
